{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import tree\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 载入数据\n",
    "data = np.genfromtxt(\"cart.csv\", delimiter=\",\")\n",
    "x_data = data[1:,1:-1]\n",
    "y_data = data[1:,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,\n",
       "            max_features=None, max_leaf_nodes=None,\n",
       "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "            min_samples_leaf=1, min_samples_split=2,\n",
       "            min_weight_fraction_leaf=0.0, presort=False, random_state=None,\n",
       "            splitter='best')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建决策树模型\n",
    "model = tree.DecisionTreeClassifier()\n",
    "# 输入数据建立模型\n",
    "model.fit(x_data, y_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'cart.pdf'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导出决策树\n",
    "import graphviz # http://www.graphviz.org/，feature_names特征名字自定义\n",
    "\n",
    "dot_data = tree.export_graphviz(model, \n",
    "                                out_file = None, \n",
    "                                feature_names = ['house_yes','house_no','single','married','divorced','income'],\n",
    "                                class_names = ['no','yes'],\n",
    "                                filled = True,\n",
    "                                rounded = True,\n",
    "                                special_characters = True)\n",
    "graph = graphviz.Source(dot_data)\n",
    "graph.render('cart')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\r\n",
       " -->\r\n",
       "<!-- Title: Tree Pages: 1 -->\r\n",
       "<svg width=\"359pt\" height=\"433pt\"\r\n",
       " viewBox=\"0.00 0.00 359.00 433.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 429)\">\r\n",
       "<title>Tree</title>\r\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-429 355,-429 355,4 -4,4\"/>\r\n",
       "<!-- 0 -->\r\n",
       "<g id=\"node1\" class=\"node\"><title>0</title>\r\n",
       "<path fill=\"#e58139\" fill-opacity=\"0.572549\" stroke=\"black\" d=\"M276.5,-425C276.5,-425 194.5,-425 194.5,-425 188.5,-425 182.5,-419 182.5,-413 182.5,-413 182.5,-354 182.5,-354 182.5,-348 188.5,-342 194.5,-342 194.5,-342 276.5,-342 276.5,-342 282.5,-342 288.5,-348 288.5,-354 288.5,-354 288.5,-413 288.5,-413 288.5,-419 282.5,-425 276.5,-425\"/>\r\n",
       "<text text-anchor=\"start\" x=\"190.5\" y=\"-409.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">married ≤ 0.5</text>\r\n",
       "<text text-anchor=\"start\" x=\"202\" y=\"-394.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.42</text>\r\n",
       "<text text-anchor=\"start\" x=\"191.5\" y=\"-379.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 10</text>\r\n",
       "<text text-anchor=\"start\" x=\"194\" y=\"-364.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [7, 3]</text>\r\n",
       "<text text-anchor=\"start\" x=\"202\" y=\"-349.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 1 -->\r\n",
       "<g id=\"node2\" class=\"node\"><title>1</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M222,-306C222,-306 117,-306 117,-306 111,-306 105,-300 105,-294 105,-294 105,-235 105,-235 105,-229 111,-223 117,-223 117,-223 222,-223 222,-223 228,-223 234,-229 234,-235 234,-235 234,-294 234,-294 234,-300 228,-306 222,-306\"/>\r\n",
       "<text text-anchor=\"start\" x=\"113\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">house_yes ≤ 0.5</text>\r\n",
       "<text text-anchor=\"start\" x=\"140\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.5</text>\r\n",
       "<text text-anchor=\"start\" x=\"130\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 6</text>\r\n",
       "<text text-anchor=\"start\" x=\"128\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [3, 3]</text>\r\n",
       "<text text-anchor=\"start\" x=\"136\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;1 -->\r\n",
       "<g id=\"edge1\" class=\"edge\"><title>0&#45;&gt;1</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M212.602,-341.907C207.687,-333.195 202.442,-323.897 197.363,-314.893\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"200.32,-313.011 192.358,-306.021 194.223,-316.45 200.32,-313.011\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"185.646\" y=\"-326.403\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\r\n",
       "</g>\r\n",
       "<!-- 6 -->\r\n",
       "<g id=\"node7\" class=\"node\"><title>6</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M339,-298.5C339,-298.5 264,-298.5 264,-298.5 258,-298.5 252,-292.5 252,-286.5 252,-286.5 252,-242.5 252,-242.5 252,-236.5 258,-230.5 264,-230.5 264,-230.5 339,-230.5 339,-230.5 345,-230.5 351,-236.5 351,-242.5 351,-242.5 351,-286.5 351,-286.5 351,-292.5 345,-298.5 339,-298.5\"/>\r\n",
       "<text text-anchor=\"start\" x=\"272\" y=\"-283.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"262\" y=\"-268.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 4</text>\r\n",
       "<text text-anchor=\"start\" x=\"260\" y=\"-253.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [4, 0]</text>\r\n",
       "<text text-anchor=\"start\" x=\"268\" y=\"-238.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;6 -->\r\n",
       "<g id=\"edge6\" class=\"edge\"><title>0&#45;&gt;6</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M258.398,-341.907C264.685,-330.763 271.513,-318.658 277.842,-307.439\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"280.926,-309.096 282.79,-298.667 274.829,-305.657 280.926,-309.096\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"289.502\" y=\"-319.049\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\r\n",
       "</g>\r\n",
       "<!-- 2 -->\r\n",
       "<g id=\"node3\" class=\"node\"><title>2</title>\r\n",
       "<path fill=\"#399de5\" fill-opacity=\"0.666667\" stroke=\"black\" d=\"M152,-187C152,-187 63,-187 63,-187 57,-187 51,-181 51,-175 51,-175 51,-116 51,-116 51,-110 57,-104 63,-104 63,-104 152,-104 152,-104 158,-104 164,-110 164,-116 164,-116 164,-175 164,-175 164,-181 158,-187 152,-187\"/>\r\n",
       "<text text-anchor=\"start\" x=\"59\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">income ≤ 77.5</text>\r\n",
       "<text text-anchor=\"start\" x=\"69.5\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.375</text>\r\n",
       "<text text-anchor=\"start\" x=\"68\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 4</text>\r\n",
       "<text text-anchor=\"start\" x=\"66\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [1, 3]</text>\r\n",
       "<text text-anchor=\"start\" x=\"71\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = yes</text>\r\n",
       "</g>\r\n",
       "<!-- 1&#45;&gt;2 -->\r\n",
       "<g id=\"edge2\" class=\"edge\"><title>1&#45;&gt;2</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M147.989,-222.907C143.373,-214.195 138.445,-204.897 133.674,-195.893\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"136.748,-194.218 128.973,-187.021 130.562,-197.496 136.748,-194.218\"/>\r\n",
       "</g>\r\n",
       "<!-- 5 -->\r\n",
       "<g id=\"node6\" class=\"node\"><title>5</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M269,-179.5C269,-179.5 194,-179.5 194,-179.5 188,-179.5 182,-173.5 182,-167.5 182,-167.5 182,-123.5 182,-123.5 182,-117.5 188,-111.5 194,-111.5 194,-111.5 269,-111.5 269,-111.5 275,-111.5 281,-117.5 281,-123.5 281,-123.5 281,-167.5 281,-167.5 281,-173.5 275,-179.5 269,-179.5\"/>\r\n",
       "<text text-anchor=\"start\" x=\"202\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"192\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2</text>\r\n",
       "<text text-anchor=\"start\" x=\"190\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [2, 0]</text>\r\n",
       "<text text-anchor=\"start\" x=\"198\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 1&#45;&gt;5 -->\r\n",
       "<g id=\"edge5\" class=\"edge\"><title>1&#45;&gt;5</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M191.011,-222.907C196.858,-211.873 203.204,-199.898 209.099,-188.773\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"212.335,-190.142 213.924,-179.667 206.149,-186.864 212.335,-190.142\"/>\r\n",
       "</g>\r\n",
       "<!-- 3 -->\r\n",
       "<g id=\"node4\" class=\"node\"><title>3</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M87,-68C87,-68 12,-68 12,-68 6,-68 0,-62 0,-56 0,-56 0,-12 0,-12 0,-6 6,-0 12,-0 12,-0 87,-0 87,-0 93,-0 99,-6 99,-12 99,-12 99,-56 99,-56 99,-62 93,-68 87,-68\"/>\r\n",
       "<text text-anchor=\"start\" x=\"20\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"10\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1</text>\r\n",
       "<text text-anchor=\"start\" x=\"8\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [1, 0]</text>\r\n",
       "<text text-anchor=\"start\" x=\"16\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 2&#45;&gt;3 -->\r\n",
       "<g id=\"edge3\" class=\"edge\"><title>2&#45;&gt;3</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M85.9029,-103.726C81.3134,-95.0615 76.4587,-85.8962 71.842,-77.1802\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"74.9118,-75.4982 67.1381,-68.2996 68.726,-78.7748 74.9118,-75.4982\"/>\r\n",
       "</g>\r\n",
       "<!-- 4 -->\r\n",
       "<g id=\"node5\" class=\"node\"><title>4</title>\r\n",
       "<path fill=\"#399de5\" stroke=\"black\" d=\"M204,-68C204,-68 129,-68 129,-68 123,-68 117,-62 117,-56 117,-56 117,-12 117,-12 117,-6 123,-0 129,-0 129,-0 204,-0 204,-0 210,-0 216,-6 216,-12 216,-12 216,-56 216,-56 216,-62 210,-68 204,-68\"/>\r\n",
       "<text text-anchor=\"start\" x=\"137\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"127\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 3</text>\r\n",
       "<text text-anchor=\"start\" x=\"125\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 3]</text>\r\n",
       "<text text-anchor=\"start\" x=\"130\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = yes</text>\r\n",
       "</g>\r\n",
       "<!-- 2&#45;&gt;4 -->\r\n",
       "<g id=\"edge4\" class=\"edge\"><title>2&#45;&gt;4</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M129.469,-103.726C134.138,-95.0615 139.076,-85.8962 143.773,-77.1802\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"146.896,-78.7632 148.558,-68.2996 140.733,-75.4428 146.896,-78.7632\"/>\r\n",
       "</g>\r\n",
       "</g>\r\n",
       "</svg>\r\n"
      ],
      "text/plain": [
       "<graphviz.files.Source at 0x2293dd2f1d0>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "graph  # 注意，每次可能变化，因为可能有两个点是同等一样的都可以\n",
    "# 结婚-->房子-->收入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
